﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Analyze.Service;
using Common.Dao;
using Common.Extension;
using log4net;
using Common.Model;
using Common.Util;
using Newtonsoft.Json;
using Spider.Mode;

namespace Analyze.Script
{
    public class NewsSegmScript
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(NewsSegmScript));

        public static void Run()
        {
            Log.Info("NewsSegmScript start");
            List<News> list = NewsDao.GetTrainingNews();

            var newsList = list.Where(m=>string.IsNullOrEmpty(m.Keywords)).ToList();

            foreach (var newse in newsList)
            {
                var dictionary = KeywordService.GetKeywords(newse.Title + newse.Abs);
                newse.Keywords = JsonConvert.SerializeObject(dictionary);
            }
            
            NewsDao.UpdateKeywords(newsList);
            
            List<DxwEvent> dxwEvents = list.Select(m=>new DxwEvent
            {
                EventId = m.ContentId,
                Block =m.BlockClassify,
                SiteTime = m.SiteTime,
                Stocks = m.Stocks,
                CrawlingTime = m.CrawlingTime,
                Content = m.Abs,
                Title = m.Title,
                Keywords = m.Keywords
            }).ToList();
            
            dxwEvents = dxwEvents.Where(m => !string.IsNullOrEmpty(m.Block)).ToList();
            DxwEventDao.BatchSaveEvents(dxwEvents);
            
            List<DxwEvent> errorEvents = new List<DxwEvent>();
            List<List<DxwEvent>> eventsList = dxwEvents.Partition(100).ToList();
            
            foreach (var events in eventsList)
            {
                List<List<string>> words = null;
                try
                {
                    words = SegmentationService.GetWords(events.Select(m => m.Title + m.Content).ToArray());
                }
                catch (Exception e)
                {
                    errorEvents.AddRange(events);
                    Log.Error("news segm error:");
                    Log.Error("events =" + errorEvents.Select(m => m.EventId).ToList().List2String());
                    continue;
                }
                
                for (var i = 0; i < events.Count; i++)
                {
                    var stringBuilder = new StringBuilder();
                    events[i].Words = Util.MergeWords(words[i]);
                }
                
                DxwEventDao.BatchSaveEventWords(events);
            }

            if (errorEvents.Count > 0)
            {
                Log.Info("retry======================================================retry");
                Log.Info("errorEvents count = " + errorEvents.Count);
                foreach (DxwEvent errorEvent in errorEvents)
                {
                    var dictionary = SegmentationService.GetWords(errorEvent.Content);

                    if (dictionary.Count == 0)
                    {
                        Log.Error("error:");
                        Log.Error("event_id=" + errorEvent.EventId);
                        Log.Error("content=" + errorEvent.Content);
                    }
                    else
                    {
                        errorEvent.Words = Util.MergeWords(dictionary);
                    }
                }
                DxwEventDao.BatchSaveEventWords(errorEvents);
            }

            list.ForEach(m=>m.IsTrainingSet = 3);
            NewsDao.BatchUpdateIsTrainingSet(list);
            Log.Info("NewsSegmScript end");
        }
        
    }
}